<?php

namespace Home\Controller;

use Think\Verify;

class UserController extends BaseController
{

    public function login()
    {
        $this->assign('wall_number', $this->wallImage());
        $this->display();
    }

    /**
         *      登录数据提交处理
         */
    public function loginSubmit()
    {
        $name = I('name');
        $password = I('password');
        $rember_password = I('rember_password', 0, 'int');
        $check_code = I('check_code');

        if (!$name || !$password) {
            $msg = L('login_not_null');
        } else {
            $name = rawurldecode($name);
            $password = rawurldecode($password);

            if(!$this->checkVerify($check_code)){
                $this->error(L('code_error'), U('User/login'));
            }

            $user = M('member')->where(array('name' => $name))->find();
            if (!$user) {
                $msg = L('user_not_exists');
            } elseif (md5($password) == $user['password']){
                if($user['status'] == 0){
                    //初始化app
                    //$app = init_controller('app');
                    //$app->init_app($user);
                }

                session('kod_login', true);
                session('kod_user', $user);
                cookie('kod_name', $user['name'], time()+3600*24*365);

                if ($rember_password == '1')
                {
                    cookie('kod_token', md5($user['password'].get_client_ip()), time()+3600*24*365);
                }
                $this->redirect(U('Index/index'));
            }else{
                $msg = L('password_error');
            }
        }
        $this->error($msg, U('User/login'));
    }


    public function checkCode() {
        $Verify = new  Verify();
        $Verify->codeSet = '123456789';
        $Verify->length = '4';
        $Verify->imageW = '130';
        $Verify->imageH = '32';
        $Verify->fontSize = '16';
        $Verify->entry();
    }

    public function commonJs()
    {
        $the_config = array(
            'lang'          => 'zh_CN',
            'is_root'       => 0,
            'web_root'      => '/',
            'web_host'      => 'http://www.mtblog.com/',
            'static_path'   => C('STATIC_PATH'),
            'basic_path'    => ROOT_PATH,
            'version'       => C('KOD_VERSION'),
            'app_host'      => ROOT_PATH,
            'office_server' => C('OFFICE_SERVER'),
            'json_data'     => "",
            'share_page'    => 'share',

            'theme'         => 'simple/',           //列表排序依照的字段
            'list_type'     => 'icon',       //列表排序依照的字段
            'sort_field'    => 'name', //列表排序依照的字段
            'sort_order'    => 'up', //列表排序升序or降序
            'musictheme'    => 'mp3player',
            'movietheme'    => 'webplayer'
        );

        $js  = 'LNG='.json_encode(L()).';';
        $js .= 'AUTH=[];';
        $js .= 'G='.json_encode($the_config).';';
        header("Content-Type:application/javascript");
        echo $js;
    }

    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串
    protected function checkVerify($code, $id = ''){
        $verify = new  Verify();
        return $verify->check($code, $id);
    }
}